<template>
  <div ref="refContainer" class="org-crumb">
    <div v-for="(item, idx) in list" :key="idx" class="crumb-item">
      <img src="../assets/icon-direct.png" />
      <div class="org-name" @click="handleOrgClick(item, idx)">{{ item.name }}</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
interface IProp {
  list: IOrgCrumb[]
}

const props = defineProps<IProp>()
const emit = defineEmits(['change'])
const refContainer = ref<NullAble<HTMLElement>>(null)

watch(
  () => props.list,
  () => {
    setTimeout(() => {
      if (refContainer.value) {
        refContainer.value.scrollLeft = refContainer.value.clientWidth
      }
    })
  }
)

const handleOrgClick = (item: string, idx: number) => {
  emit('change', item, idx)
}
</script>

<style lang="less" scoped>
.org-crumb {
  flex-shrink: 0;
  overflow-x: scroll;
  width: 100%;
  white-space: nowrap;
  box-sizing: border-box;
  height: 52px;
  padding: 12px 12px 0px 12px;

  &::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
  .crumb-item {
    display: inline-flex;
    align-items: center;
    color: #212121;
    font-size: 13px;

    img {
      width: 7px;
      height: 9px;
      margin: 0px 7.5px;
    }

    &:first-child {
      img {
        display: none;
      }
    }

    &:last-child {
      color: #fff;

      .org-name {
        background: #3a75c5;
        box-shadow: 0px 2px 5px 0px #3a75c519;
        border-radius: 25px;
        height: 28px;
        line-height: 28px;
        padding: 0px 8px;
      }
    }
  }
}
</style>
